﻿Imports OMANE.Common.UI
Imports System.Windows.Input
Imports PTIC_Util
Imports Actuariat

Namespace ViewModels
    Public Class ViewModelResCRT
        Inherits ViewModelCommand
        Implements IViewModelTabSection

        Sub New()
        End Sub
        Private _Model As ModelTauxGarantis
        Public Sub New(ByVal Model As ModelTauxGarantis, ByVal DisplayName As String, ByVal Command As ICommand)
            MyBase.DisplayName = DisplayName
            _Model = Model
            AddHandler _Model.ListeTraitements.TraitMoteurMethode2.SegmentationRetenueChanged, AddressOf MoteurMethode2_SegmentationChanged
            AddHandler _Model.ListeTraitements.TraitMoteurMethode2.NbGenerationsProjeteesChanged, AddressOf MoteurMethode2_IdGenerationChanged
            AddHandler _Model.ListeTraitements.TraitMoteurMethode2.Comptes.EtatCalculChanged, AddressOf ComptesMoteurMethode2_AfterCalculated
            Title = "COMPTES DE RESULTAT"
        End Sub

        Public Property Icone As String Implements Common.UI.IViewModelTabSection.Icone
        Public Property Title As String Implements Common.UI.IViewModelTabSection.Title
        Public Property IsClosable As Boolean Implements IViewModelTabSection.IsClosable

#Region "Choix du compte à afficher"
        ''' <summary>
        ''' Liste des grilles de compte disponibles
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property ListeGrillesComptes As List(Of VMGrilleCompte)
            Get
                If _ListeGrillesComptes Is Nothing Then
                    _ListeGrillesComptes = New List(Of VMGrilleCompte)
                    _ListeGrillesComptes.Add(New VMGrilleCompteResultat)
                    _ListeGrillesComptes.Add(New VMGrilleCompteBilan)
                    _ListeGrillesComptes.Add(New VMGrilleCompteIndicateurs)
                End If
                Return _ListeGrillesComptes
            End Get
            Set(ByVal value As List(Of Actuariat.VMGrilleCompte))
                OnPropertyChanging("ListeGrillesComptes")
                Dim e As New PropertyChangeEventArgs(Of List(Of Actuariat.VMGrilleCompte))(ListeGrillesComptes, value)
                If e.IsValide Then RaiseEvent ListeGrillesComptesChanging(Me, e)
                If e.IsValide Then
                    _ListeGrillesComptes = e.NewValue
                    OnPropertyChanged("ListeGrillesComptes")
                    RaiseEvent ListeGrillesComptesChanged(Me, e)
                End If
            End Set
        End Property
        Private _ListeGrillesComptes As List(Of Actuariat.VMGrilleCompte)
        Public Event ListeGrillesComptesChanging As PropertyChangeEventHandler(Of List(Of Actuariat.VMGrilleCompte))
        Public Event ListeGrillesComptesChanged As PropertyChangeEventHandler(Of List(Of Actuariat.VMGrilleCompte))

        Private _SelectedCompte As VMGrilleCompte
        Public Property SelectedCompte As VMGrilleCompte
            Get
                If _SelectedCompte Is Nothing Then
                    _SelectedCompte = ListeGrillesComptes(0)
                End If
                Return _SelectedCompte
            End Get
            Set(ByVal value As VMGrilleCompte)
                OnPropertyChanging("SelectedCompte")
                Dim e As New PropertyChangeEventArgs(Of VMGrilleCompte)(SelectedCompte, value)
                If e.IsValide Then RaiseEvent SelectedCompteChanging(Me, e)
                If e.IsValide Then
                    _SelectedCompte = e.NewValue
                    OnPropertyChanged("SelectedCompte")
                    RaiseEvent SelectedCompteChanged(Me, e)
                End If
            End Set
        End Property
        Public Event SelectedCompteChanging As PropertyChangeEventHandler(Of VMGrilleCompte)
        Public Event SelectedCompteChanged As PropertyChangeEventHandler(Of VMGrilleCompte)
#End Region

#Region "Paramétrage des PM"
        Private _ListeBooleanCbx As List(Of String)
        Public ReadOnly Property ListeBooleanCbx As List(Of String)
            Get
                If _ListeBooleanCbx Is Nothing Then
                    _ListeBooleanCbx = New List(Of String)
                    _ListeBooleanCbx.Add("Oui")
                    _ListeBooleanCbx.Add("Non")
                End If
                Return _ListeBooleanCbx
            End Get
        End Property

        Public Property HasPMRente As String
            Get
                Return(IIf(Me.ComptesMoteur2.HasPMrente, "Oui", "Non"))
            End Get
            Set(ByVal value As String)
                OnPropertyChanging("HasPMRente")
                If value = "Oui" Then
                    Me.ComptesMoteur2.HasPMrente = True
                Else
                    Me.ComptesMoteur2.HasPMrente = False
                End If
                OnPropertyChanged("HasPMRente")
                OnPropertyChanged("TRI")
                OnPropertyChanged("VANoperation")
                OnPropertyChanged("VANprimes")
            End Set
        End Property
        Public Property HasPPRC As String
            Get
                Return IIf(Me.ComptesMoteur2.HasPPRC, "Oui", "Non")
            End Get
            Set(ByVal value As String)
                OnPropertyChanging("HasPPRC")
                If value = "Oui" Then
                    Me.ComptesMoteur2.HasPPRC = True
                Else
                    Me.ComptesMoteur2.HasPPRC = False
                End If
                OnPropertyChanged("TRI")
                OnPropertyChanged("VANoperation")
                OnPropertyChanged("VANprimes")
                OnPropertyChanged("HasPPRC")
            End Set
        End Property
        Public Property HasPMdc As String
            Get
                Return IIf(Me.ComptesMoteur2.HasPMdc, "Oui", "Non")
            End Get
            Set(ByVal value As String)
                OnPropertyChanging("HasPMdc")
                If value = "Oui" Then
                    Me.ComptesMoteur2.HasPMdc = True
                Else
                    Me.ComptesMoteur2.HasPMdc = False
                End If
                OnPropertyChanged("HasPMdc")
                OnPropertyChanged("TRI")
                OnPropertyChanged("VANoperation")
                OnPropertyChanged("VANprimes")
            End Set
        End Property
#End Region

#Region "Choix de l'identifiant segment à afficher"
        Private _ListeIdSegments As List(Of String)
        Public ReadOnly Property ListeIdSegments As List(Of String)
            Get
                If _ListeIdSegments Is Nothing Then
                    _ListeIdSegments = New List(Of String)
                    MAJ_ListeIdSegments()
                End If
                Return _ListeIdSegments
            End Get
        End Property
        Public Property SelectedIdSegment As String
            Get
                With _Model.ListeTraitements.TraitMoteurMethode2
                    Return IIf(.filtreSegment = 0, "Tous", .filtreSegment)
                End With
            End Get
            Set(ByVal value As String)
                OnPropertyChanging("SelectedIdSegment")
                With _Model.ListeTraitements.TraitMoteurMethode2
                    If value = "Tous" Then
                        .filtreSegment = 0
                    Else
                        .filtreSegment = CInt(value)
                    End If
                End With
                OnPropertyChanged("SelectedIdSegment")
            End Set
        End Property

        Private Sub MoteurMethode2_SegmentationChanged(ByVal sender As Object, ByVal e As EventArgs)
            MAJ_ListeIdSegments()
        End Sub
        Private Sub MAJ_ListeIdSegments()
            'Mettre à jour la liste de choix des générations visualisables
            OnPropertyChanging("ListeIdSegments")

            Dim nbSegments As Integer
            Dim table As DataTable
            With _Model.ListeTraitements.TraitReqSegmentation
                If .EtatCalcul_DataTable <> ValueEtatCalcul.A_JOUR Then
                    .Calculer_DataTable()
                End If
                table = .DataTableResultat
            End With
            If Not table Is Nothing Then
                nbSegments = table.Rows.Count
            Else
                nbSegments = 0
            End If

            _ListeIdSegments.Clear()
            For i As Integer = 1 To nbSegments
                _ListeIdSegments.Add(CStr(i))
            Next
            _ListeIdSegments.Add("Tous")
            OnPropertyChanged("ListeIdSegments")
        End Sub

#End Region

#Region "Choix de la génération à afficher"
        Private _ListeIdGenerations As List(Of String)
        Public ReadOnly Property ListeIdGenerations As List(Of String)
            Get
                If _ListeIdGenerations Is Nothing Then
                    _ListeIdGenerations = New List(Of String)
                    MAJ_ListeIdGenerations()
                End If
                Return _ListeIdGenerations
            End Get
        End Property
        Public Property SelectedIdGeneration As String
            Get
                With _Model.ListeTraitements.TraitMoteurMethode2
                    Return IIf(.filtreGeneration = 0, "Tous", .filtreGeneration)
                End With
            End Get
            Set(ByVal value As String)
                OnPropertyChanging("_SelectedIdGeneration")
                With _Model.ListeTraitements.TraitMoteurMethode2
                    If value = "Tous" Then
                        .filtreGeneration = 0
                    Else
                        .filtreGeneration = CInt(value)
                    End If
                End With
                OnPropertyChanged("SelectedIdGeneration")
            End Set
        End Property

        Private Sub MoteurMethode2_IdGenerationChanged(ByVal sender As Object, ByVal e As EventArgs)
            MAJ_ListeIdGenerations()
        End Sub
        Private Sub MAJ_ListeIdGenerations()
            'Mettre à jour la liste de choix des générations visualisables
            OnPropertyChanging("ListeIdGenerations")
            Dim nbGenerations As Integer = _Model.ListeTraitements.TraitMoteurMethode2.nbGenProj
            _ListeIdGenerations.Clear()
            For i As Integer = 1 To nbGenerations
                _ListeIdGenerations.Add(CStr(i))
            Next
            _ListeIdGenerations.Add("Tous")
            OnPropertyChanged("ListeIdGenerations")
        End Sub
#End Region

#Region "Indicateurs (TRI / VANs)"
        Public ReadOnly Property TRI As Double
            Get
                Return ComptesMoteur2.Indicateur.Resultats.TRI
            End Get
        End Property
        Public ReadOnly Property VANoperation As Double
            Get
                Return ComptesMoteur2.Indicateur.Resultats.VAN
            End Get
        End Property
        Public ReadOnly Property VANprimes As Double
            Get
                Return ComptesMoteur2.Indicateur.Resultats.VANprimes
            End Get
        End Property
#End Region

#Region "Nombre d'années de projection"
        Public Property NombreAnneesProjection As Integer
            Get
                Return _Model.ListeTraitements.TraitMoteurMethode2.Comptes.NombreAnneesProjection
            End Get
            Set(ByVal value As Integer)
                OnPropertyChanging("NombreAnneesProjection")
                _Model.ListeTraitements.TraitMoteurMethode2.Comptes.NombreAnneesProjection = value
                OnPropertyChanged("NombreAnneesProjection")
            End Set
        End Property
#End Region

#Region "Unité de calcul"
        Private _ListeUnites As List(Of String)
        Public ReadOnly Property ListeUnites As List(Of String)
            Get
                If _ListeUnites Is Nothing Then
                    _ListeUnites = New List(Of String)
                    _ListeUnites.Add("€")
                    _ListeUnites.Add("K€")
                    _ListeUnites.Add("M€")
                End If
                Return _ListeUnites
            End Get
        End Property


        Public Property SelectedUnite As String
            Get
                If Me.ComptesMoteur2.Unite = 1000 Then
                    Return "K€"
                ElseIf Me.ComptesMoteur2.Unite = 1000000 Then
                    Return "M€"
                Else
                    Return "€"
                End If
            End Get
            Set(ByVal value As String)
                If SelectedUnite <> value Then
                    OnPropertyChanging("SelectedUnite")
                    If value = "K€" Then
                        Me.ComptesMoteur2.Unite = 1000
                    ElseIf value = "M€" Then
                        Me.ComptesMoteur2.Unite = 1000000
                    Else
                        Me.ComptesMoteur2.Unite = 1
                    End If
                    OnPropertyChanged("SelectedUnite")
                End If
            End Set
        End Property

#End Region
        'Public Property PremiereAnneeProjection As Integer
        '    Get
        '        Return _Model.Univers.listeTraitements.TraitMoteurMethode2.Comptes.AnneePremiereGeneration
        '    End Get
        '    Set(ByVal value As Integer)
        '        OnPropertyChanging("PremiereAnneeProjection")
        '        _Model.Univers.listeTraitements.TraitMoteurMethode2.Comptes.AnneePremiereGeneration = value
        '        OnPropertyChanged("PremiereAnneeProjection")
        '    End Set
        'End Property

        Private Sub ComptesMoteurMethode2_AfterCalculated(ByVal sender As Object, ByVal e As EventArgs)
            OnPropertyChanging("NombreAnneesProjection")
            OnPropertyChanged("TRI")
            OnPropertyChanged("VANoperation")
            OnPropertyChanged("VANprimes")
            OnPropertyChanged("HasPMRente")
            OnPropertyChanged("HasPPRC")
            OnPropertyChanged("HasPMdc")
        End Sub

        Public ReadOnly Property ComptesMoteur2 As Actuariat.Comptes.ComptesMoteur2
            Get
                Dim comptes As Comptes.ComptesMoteur2 = _Model.ListeTraitements.TraitMoteurMethode2.Comptes
                Return comptes
            End Get
        End Property
    End Class
End Namespace
